/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package engine.linkList;

import java.io.Serializable;


/**
 *
 * @author hjp
 */
public abstract class AbstractList<T extends AbstractNode>  implements  Serializable {
    //abstract void add(Node<T> node);
    //abstract Node<MusicListNode> search(String key);

    public Node<T> head;
    public Node<T> tail;
    public int sum;
    public boolean isModify;

    /**
     * 在链表头部添加结点
     */
    void addHead(Node<T> node){
        if (head == null){
            head = tail = node;
        }
        else {
            head.addPre(node);
            head = node;
        }
        isModify = true;
        sum++;
    }

    /**
     * 在链表尾部添加结点
     */
    void addTail(Node<T> node){
        if (head == null){
            head = tail = node;
        }
        else {
            tail.addNext(node);
            tail = node;
        }
        isModify = true;
        sum++;
    }

    /**
     * 删除整个链表
     */
    void delete(){
        sum = 0;
        Node<T> p = head;
        Node<T> q;
        while(p != null){
            q = p.next;
            p.delete();
            p = q;
        }
        head = tail = null;
        isModify = true;
    }

    /**
     * 从给定的AbstractList中删除第index个结点
     */
    void delete(int index){
        Node<T> p = head;
        if (sum == 1){
            head = tail = null;
        }
        else if (index == 1){
            head = head.next;
        }
        else if (index == sum){
            p = tail;
            tail = tail.pre;
        }
        else {
            --index;
            while (index != 0){
                p = p.next;
                --index;
            }
        }
        sum--;
        isModify = true;
        p.delete();
    }

//    String[] print(Node<T> head,int sum){
//        String[] info = new String[sum];
//        int i = -1;
//        Node<T> p = head;
//        while (p != null){
//            info[++i] = p.toString();
//            p = p.next;
//        }
//        return info;
//    }
}
